import Vue from "vue";
import Vuex from "vuex";
import router from "../router";
//引入自定义路由
import allrouters from "../router/allrouters";
//引入动态路由
import dynamicRoutes from "../router/dynamicRoutes";
//引入计算侧边栏需要的路由的方法
import recurisonRouters from "../utils/recurisonRouters";
import { getMenulist, getstudata, viporder } from "../api/index";
Vue.use(Vuex);
//从本地存储获取用户信息用于login组件
let nickname = localStorage.getItem("userinfo") || "{}";
//从本地存储获取预支付的订单
let currentproduct = localStorage.getItem("currentchargerproduct") || "[]";
export default new Vuex.Store({
  state: {
    userinfo: JSON.parse(nickname), //存放用户信息
    sideMenu: [], //根据登录用户所要匹配的侧边栏路由
    breadcrumb: [], //面包屑数据
    dialogFormVisible: false, //dialog显示隐藏
    dialogTitle: "", //更改dialog标题
    studata: [], //存放学员项目表格数据
    loading: true, //loading加载方法
    editdialog: {}, //编辑按钮索要回显的数据
    pagetotal: 0, //分页总条目
    orderData: [], //VIP订单数据
    order_id: "", //后台返回的订单id
    currentchargerproduct: JSON.parse(currentproduct), //当前充值的产品
    pagesize: 10, //每页显示的数量根据getstudata传的参数来定
  },
  mutations: {
    //把login组件传进来的登录用户存入state中
    SET_USERINFO(state, payload) {
      state.userinfo = payload;
    },
    //处理侧边栏路由
    SET_MENULIST(state, payload) {
      //将计算出来的侧边栏路由添加到dynamicRoutes中的‘/’路径配置中的children属性中
      let target = dynamicRoutes.find((item) => {
        return item.path === "/";
      });
      //添加了vip管理充值路由后的'/'子路由的修改
      target.children = [...payload, ...target.children];
      //核心方法router.addRoutes([])
      //要将我们说需要的路由添加到路由index文件中的routes数组中
      dynamicRoutes.forEach((item)=>{
        router.addRoute(item)
      })
      //router.addRoutes(dynamicRoutes);
      //预支付订单页面不需要展示在菜单栏
      //要排除不需要展示在菜单中的页面pre_ order payment
      let excludeRoutes = ["pre_order", "payment"];
      target.children = target.children.filter((item) => {
        return !excludeRoutes.includes(item.path);
      });
      //将处理好的动态路由数组放入vuex中
      state.sideMenu = target.children;
    },

    //设置面包屑
    SET_BREADCRUMBS(state, payload) {
      state.breadcrumb = payload;
    },
    //控制dialog显示方法
    SHOW_DIALOGVISIBLE(state, payload) {
      state.dialogFormVisible = true;
    },
    //控制dialog隐藏方法
    HIDE_DIALOGVISIBLE(state, payload) {
      state.dialogFormVisible = false;
    },
    //修改dialog标题
    CHANGE_DAILOGTITLE(state, payload) {
      state.dialogTitle = payload;
    },
    //保存学员项目管理表格中的数据到vuex
    SET_STUDATA(state, payload) {
      state.studata = payload.data;
      state.pagetotal = payload.total;
    },
    //修改编辑dailog回显数据
    SET_BACKDATA(state, payload) {
      state.editdialog = {
        ...payload,
      }; //浅拷贝不能直接赋值，复杂数据内存地址
    },
    //保存vip订单数据
    SET_VIPORDER(state, payload) {
      state.orderData = payload;
    },
    //设置当前充值的产品
    SET_PREORDER(state, payload) {
      //以为是push进数组所以会有两个订单
      //state.currentchargerproduct.push(payload)
      state.currentchargerproduct = [payload];
      localStorage.setItem("currentchargerproduct", JSON.stringify([payload]));
    },
    //保存预支付订单id
    SET_ORDERID(state, payload) {
      state.order_id = payload;
    },
  },
  actions: {
    //获取服务器的中的当前用户菜单
    async GET_MENULIST(context, payload) {
      //结构服务器响应数据中的menuList
      let res = await getMenulist();
      let { menuList } = res.data;
      //根据后台返回的用户菜单对比所有路由取出路由元信息中name与返回数据name相同的选项
      let userRouter = recurisonRouters(menuList, allrouters);  
      context.commit("SET_MENULIST", userRouter);
    },

    //获取学员项目表格数据,payload没有传参就默认是事件对象所以就没有pages等属性
    GET_STUDATA(context, payload = {}) {
      let { pages = 1, count = 10, classes = "" } = payload;
      getstudata(pages, count, classes).then((res) => {
        context.commit("SET_STUDATA", res.data);
        //获取数据后关闭loading加载
        context.state.loading = false;
      });
    },
    //获取vip订单数据
    GET_VIPORDER(context, payload) {
      let orderid = payload;
      viporder(orderid).then((res) => {
        console.log(res.data);
        if (res.data.state) {
          context.commit("SET_VIPORDER", res.data.data);
        } else {
          this.$message.error("获取订单失败");
        }
      });
    },
  },
  modules: {},
});
